package com.hankarun.gevrek;

import java.util.List;
import com.hankarun.gevrek.lib.DataSource;
import com.hankarun.gevrek.lib.NewsGroupName;
import com.hankarun.gevrek.lib.ServerManager;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;

public class GroupActivity extends Activity {
	private ListView groupList;
	private DataSource datasource;
	private ArrayAdapter<NewsGroupName> adapter;
	protected ServerManager app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        
        datasource = new DataSource(this);
		datasource.open();
		List<NewsGroupName> values = datasource.getAllGroups();
        datasource.close();
        
        app = (ServerManager)getApplication();
        
        if(values.size() == 0){
        	Toast.makeText(getApplicationContext(), R.string.nonewsgroupselected , Toast.LENGTH_SHORT).show();
        }
        
        groupList = (ListView) findViewById(R.id.newsGroupListView);
        
        adapter = new ArrayAdapter<NewsGroupName>(this,
				android.R.layout.simple_list_item_1, values);
        
        groupList.setAdapter(adapter);
        
        groupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				app.setGroup(adapter.getItem(arg2).getGroup());
		    	Intent myIntent = new Intent(GroupActivity.this, MessageListActivity.class);
		    	GroupActivity.this.startActivityForResult(myIntent, 1);
			}
          });
        
        Button select = (Button) findViewById(R.id.addNewsGroupButton);
        select.setOnClickListener(new Button.OnClickListener() { 
        	public void onClick (View v){ 
		    	Intent myIntent = new Intent(GroupActivity.this, GroupSelect.class);
		    	GroupActivity.this.startActivityForResult(myIntent, 0);
        		}
        	});
        
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1){       	
    		datasource.open();            
            List<NewsGroupName> values = datasource.getAllGroups();
            datasource.close();
            adapter.clear();
            if(values.size() == 0){
            	Toast.makeText(getApplicationContext(), R.string.nonewsgroupselected , Toast.LENGTH_SHORT).show();
            }else{            	
            	for(NewsGroupName obj : values)
            		adapter.add(obj);            	
            }
            adapter.notifyDataSetChanged();
        }
    }
    
    @Override
    public void onBackPressed() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                GroupActivity.this);

        alertDialog.setPositiveButton(R.string.yes, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        alertDialog.setNegativeButton(R.string.no, null);

        alertDialog.setMessage(R.string.doyouwantlogout);
        alertDialog.setTitle(R.string.app_name);
        alertDialog.show();
    }
    
    @Override
    protected void onPause() {
        super.onPause();

        setResult(0);
    }
    
    @Override
    protected void onStop() {
        super.onPause();

        setResult(0);
    }
}
